﻿#include "../app.h"
#include "../app_config.h"
#include "app_system.h"
#include "../menu_bar/menu_bar.h"

static app_system_t app_system;
static lv_style_t style;

static void on_event(lv_event_t* event) {
    app_switch_by_name("app_template");
}

/**
 * @brief 主要用于加载/初始化数据和界面
 * @param content 上下文
*/
static void app_load(void* content) {
    app_page_t* page = (app_page_t*)content;

    // 1. 数据初始化
    page->user_data = "page data";

    // 2. UI初始化
    lv_style_init(&style);
    lv_style_set_bg_color(&style, lv_color_black());
    lv_style_set_pad_top(&style, 10);
    lv_style_set_pad_bottom(&style, 10);
    lv_style_set_pad_left(&style, 10);
    lv_style_set_pad_right(&style, 10);
    lv_style_set_text_color(&style, lv_palette_main(LV_PALETTE_RED));
    lv_style_set_text_letter_space(&style, 0);
    lv_style_set_text_line_space(&style, 20);
    lv_style_set_text_decor(&style, LV_TEXT_DECOR_NONE);
    lv_style_set_text_font(&style, &lv_font_montserrat_16);
    lv_style_set_text_color(&style, lv_color_white());

    if (page->root == NULL)
    {
        app_log_error("page root is null! please check memory.");
    }
    lv_obj_add_style(page->root, &style, 0);
    // 采用基础布局
    lv_obj_set_layout(page->root, LV_LAYOUT_FLEX);
    lv_obj_set_flex_flow(page->root, LV_FLEX_FLOW_ROW_WRAP);

    app_system.device = lv_label_create(page->root);
    lv_obj_add_style(app_system.device, &style, 0);
    lv_label_set_text_fmt(app_system.device, "Device : %s", DEVICE_NAME);

    app_system.model = lv_label_create(page->root);
    lv_obj_add_style(app_system.model, &style, 0);
    lv_label_set_text_fmt(app_system.model, "Model : %s", DEVICE_MODEL);

    app_system.mac = lv_label_create(page->root);
    lv_obj_add_style(app_system.mac, &style, 0);
    lv_label_set_text_fmt(app_system.mac, "MAC : %02X-%02X-%02X-%02X-%02X-%02X",
        0x12, 0x34, 0x56, 0x78, 0x90, 0xAB);

    app_system.sn = lv_label_create(page->root);
    lv_obj_add_style(app_system.sn, &style, 0);
    lv_label_set_text_fmt(app_system.sn, "SN : %d", 12345678);

    app_system.sw_ver = lv_label_create(page->root);
    lv_obj_add_style(app_system.sw_ver, &style, 0);
    lv_label_set_text_fmt(app_system.sw_ver, "Version : %s", SOFTWARE_VERSION);

    app_system.built_time = lv_label_create(page->root);
    lv_obj_add_style(app_system.built_time, &style, 0);
    lv_label_set_text_fmt(app_system.built_time, "Built at : %s, %s", __TIME__, __DATE__);

    app_system.author = lv_label_create(page->root);
    lv_obj_add_style(app_system.author, &style, 0);
    lv_label_set_text_fmt(app_system.author, "Author : %s", MANUFACTURER_NAME);

    app_system.back = lv_btn_create(page->root);
    lv_obj_t* l = lv_label_create(app_system.back);
    lv_label_set_text(l, "to template");
    lv_obj_add_event_cb(app_system.back, on_event, LV_EVENT_CLICKED, NULL);
    // 3. 事件初始化
}
static void app_did_load(void* content) {
    app_page_t* page = (app_page_t*)content;
    //lv_obj_fade_in(page->root, 500, 0);
}
static void app_will_appear(void* content) {
    app_page_t* page = (app_page_t*)content;
    //lv_obj_fade_in(page->root, 500, 0);
}
static void app_did_appear(void* content) {
    menu_bar_appear(false);
}
static void app_will_disappear(void* content) {
    app_page_t* page = (app_page_t*)content;
    //lv_obj_fade_out(page->root, 500, 0);
}
static void app_did_disappear(void* content) {

}
static void app_did_unload(void* content) {
    app_page_t* page = (app_page_t*)content;
    // 移除界面
    lv_style_reset(&style);
    // 移除数据
}

static app_ops_t app_ops =
{
    .on_load = app_load,
    .on_did_load = app_did_load,
    .on_will_appear = app_will_appear,
    .on_did_appear = app_did_appear,
    .on_will_disappear = app_will_disappear,
    .on_did_disappear = app_did_disappear,
    .on_did_unload = app_did_unload
};

app_page_t app_page_system =
{
    .root = NULL,
    .user_data = NULL,
    .anim.is_busy = false,
    .anim.is_enter = false,
    .id = 1,
    .name = "system",
    .ops = &app_ops,
    .state = PAGE_STATE_IDLE
};
